package com.situ.stmall.front.mapper;

import com.situ.stmall.front.entity.Goods;
import org.apache.ibatis.annotations.*;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Mapper
public interface GoodsMapper {
//    /**
//     * 插入
//     */
//    @Insert({"insert into goods",
//            "(name,dscp,price,mark_price,color,version,count,content,recom,category_id)",
//            "value(#{name},#{dscp},#{price},#{markPrice},#{color},#{version},#{count},#{content},#{recom},#{categoryId})"})
//    @Options(useGeneratedKeys = true,keyColumn = "id",keyProperty = "id")
//    int insert(Goods goods);
//    /**
//     * 根据id删除
//     */
//    @Delete("delete from goods where id = #{id}")
//    int delete(Integer id);
//    /**
//     * 根据id更新
//     */
//    @Update({"<script>",
//            "update goods",
//            "<set>",
//            "<if test='name !=null and name.length>0' > name=#{name}, </if>",
//            "<if test='dscp !=null and dscp.length>0' > dscp=#{dscp}, </if>",
//            "<if test='price !=null' > price=#{price}, </if>",
//            "<if test='markPrice !=null' > mark_price=#{markPrice}, </if>",
//            "<if test='color !=null and color.length>0' > color=#{color}, </if>",
//            "<if test='version !=null and version.length>0' > version=#{version}, </if>",
//            "<if test='count !=null' > count=#{count} ,</if>",
//            "<if test='content !=null and content.length>0' > content=#{content}, </if>",
//            "<if test='recom !=null' > recom=#{recom}, </if>",
//            "<if test='categoryId !=null' > category_id=#{categoryId}, </if>",
//            "</set>",
//            "where id = #{id}",
//            "</script>"})
//    int update(Goods goods);
    /**
     * 根据id查询
     */
    @Select("select * from goods where id = #{id}")
    @ResultMap("goods")
    Goods selectById(Integer id);
    /**
     * 多条件联合查询
     */
    @Select({"<script>",
            "select * from goods",
            "inner join category on goods.category_id = category.id",
            "inner join category as p on category.parent_id = p.id",
            "<where>",
            "<if test='where.name !=null and where.name.length>0' > goods.name like '%${where.name}%'</if>",
            "<if test='where.categoryId !=null' >and (category_id=#{where.categoryId} or category.parent_id=#{where.categoryId})</if>",
            "and goods.status = 0",
            "and category.status = 0",
            "and p.status = 0",
            "</where>",
            "<if test='orderBy !=null and orderBy.length>0'>order by ${orderBy}</if>",
            "</script>"})
    @Results(id="goods",
            value={
            @Result(column = "id",property = "id",id = true),
            @Result(column = "id",property = "pics",
                    many = @Many(select = "com.situ.stmall.front.mapper.GoodsPicMapper.selectByGoodsId")),
    })
    List<Goods> select(@Param("where") Goods where,@Param("orderBy") String orderBy);
}
